package com.dzqc.fly.common;

import org.apache.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：数据分装工具
 * author:tonghl
 * Date：2022年6月28日
 */
public class Result extends HashMap<String, Object> {
    public Result() {
        put("code", HttpStatus.SC_OK);
        put("msg", "success");
    }

    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static Result ok() {
        return new Result();
    }

    public static Result ok(String msg) {
        Result r = new Result();
        r.put("msg", msg);
        return r;
    }

    public static Result ok(Map<String, Object> map) {
        Result r = new Result();
        r.putAll(map);
        return r;
    }
    public static Result commonOk(Object data) {
        Result r = new Result();
        r.put("data",data);
        r.put("code", HttpStatus.SC_OK);
        r.put("msg", "success");
        return r;
    }
    public static Result commonOk(Object data,String msg) {
        Result r = new Result();
        r.put("data",data);
        r.put("code", HttpStatus.SC_OK);
        r.put("msg", msg);
        return r;
    }

    public static Result error(int code, String msg) {
        Result r = new Result();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static Result error(String msg) {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    public static Result error() {
        return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常，请联系管理员");
    }

}
